Action
An action is POJO. It does not have to implement any interface. Of course, it has to implement the method specified in WEB-INF/zest
. For example,
package foo;
public class HelloAction {
private String _message = "Welcome";
/** The execute method specified in WEB-INF/zest.xml. */
public String execute() {
return "success";
}
/** Sets the message. */
public void setMessage(String message) {
_message = message;
}
/** Returns the message. */
public String getMessage() {
return _message;
}
}
Signature of the execute Method
There are two kinds of signatures are allowed for the execute method of an action.
public String execute();
public String execute(ActionContext ac);
The method's name could be anything you prefer, as long as it is the same as the one you specified in WEB-INF/zest.xml
.
The signature with ActionContext has higher priority if both method are specified (and the signature with no arg will be ignored). It is used if you need the information of the request, such as HttpServletRequest
.
public class FooAction {
public String execute(ActionContext ac) {
HttpServletRequest request = ac.getServletRequest();
....
Parameters of Request
If a parameter of the request (ServletRequest.getParameterMap()
) matches a setter method of the action, the method will be invoked to set the parameter's value. For example, assume the action has a method called void setIndex(int index)
and the request contains a parameter, index=5 (such as http://foo.com/foo?index=5), then setIndex(5)
will be invoked. Thus, you could retrieve the value directly from the action, such as int getIndex()
.
Handling Conversion Errors
If ZEST fails to coerce a parameter to the setter method's argument, an exception will be thrown. By default, the exception will be ignored (i.e., no error, no log). If you'd like to handle it, you could implement an error handle, ErrorHandler, and specifies it in the error-handler-class element in WEB-INF/zest.xml
.
Disabling Parameter Conversion
If you prefer not to convert the parameters to the action, you could implement ParameterIgnored in the action. For example,
public class FooAction implements ParameterIgnored {
...
Version History
Version | Date | Content |
---|---|---|